package cn.itcast.bos.service.qp.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.bc.DecidedZoneDao;
import cn.itcast.bos.dao.bc.RegionDao;
import cn.itcast.bos.dao.qp.NoticeBillDao;
import cn.itcast.bos.dao.qp.WorkBillDao;
import cn.itcast.bos.domain.bc.DecidedZone;
import cn.itcast.bos.domain.bc.Region;
import cn.itcast.bos.domain.bc.Staff;
import cn.itcast.bos.domain.bc.Subarea;
import cn.itcast.bos.domain.bc.customer.Customer;
import cn.itcast.bos.domain.qp.NoticeBill;
import cn.itcast.bos.domain.qp.WorkBill;
import cn.itcast.bos.service.qp.NoticeBillService;

@Service("noticeBillService")
@Transactional
public class NoticeBillServiceImpl implements NoticeBillService {

	// 1: 引入 Crm 系统 CustomerService
	@Autowired
	private NoticeBillDao noticeBillDao;
	@Autowired
	private DecidedZoneDao decidedZoneDao;
	@Autowired
	private RegionDao regionDao;

	@Autowired
	private WorkBillDao workBillDao;// 工单生成

	// 工单生成
	/**
	 * private String id; private NoticeBill noticeBill; private Staff staff; private String type; private String pickstate; private Date buildtime; private BigDecimal attachbilltimes; private String remark;
	 * 
	 */

	public void add(NoticeBill model, String province, String city, String district, String detail, Customer c) {
		// 1: 新单添加业务 插入业务通知单
		noticeBillDao.save(model);// staff_id (实现自动 ) user_id(session existUser ) ordertype(自动 / 人工)
		// 2: 1: 自动分单 crm 系统地址查询 完全匹配
		if (c != null) {
			// 地址完全 相同 自动分单
			String decidedZone_id = c.getDecidedZone_id();// 客户查询 关联定区id
			// decidedZone_id 定区id 是否为空 ....先做判断
			if (StringUtils.isNotBlank(decidedZone_id)) {
				// 不一定发送sql 语句
				DecidedZone decidedZone = decidedZoneDao.findOne(decidedZone_id);
				// 定区是否为空 判断自动分单
				// 实现自动分单
				Staff staff = decidedZone.getStaff();
				model.setStaff(staff);// 业务通知单 给到取派员
				model.setOrdertype("自动");
				getWorkBill(model, staff);
				return;
			}
		}
		// 2: 根据省市区 查询 region--->subareas--->模糊匹配关键字--->subarea--->decidedzone --->staff
		Region r = regionDao.findRegionByProvinceAndCityAndDistrict(province, city, district);
		if (r != null) {
			// 查询询区域 ---> 分区 --->关键字 辅助关键字...分区---> 定区 --->取派员
			Set<Subarea> subareas = r.getSubareas();// 当前区域下所有分区
			for (Subarea subarea : subareas) {
				// 模糊关键字匹配 detail 模糊字符串 subarea.getAddressKey 关键字
				if (detail.contains(subarea.getAddresskey())) {
					// 如果模糊查询到 省市区 关键字 --->分区
					DecidedZone zone = subarea.getDecidedZone();// zone 有可能找到分区 没有关联定区
					// 自动分单 找到定区
					if (zone != null) {
						// 自动分单
						Staff staff = zone.getStaff();
						model.setStaff(staff);// 业务通知单 给到取派员
						model.setOrdertype("自动");
						getWorkBill(model, staff);
						return;
					}
				}
			}
		}

		// 3: 人工分单 另一个业务模块
		model.setOrdertype("人工");// 要不要生成工单? 自动分单业务 不需要生成 工单!

	}

	private void getWorkBill(NoticeBill model, Staff staff) {
		WorkBill w = new WorkBill();
		w.setNoticeBill(model);
		w.setPickstate("新单");
		w.setAttachbilltimes(new BigDecimal(0));
		w.setBuildtime(new Date());
		w.setStaff(staff);
		w.setType("新");
		w.setRemark(model.getRemark());
		workBillDao.save(w);
	}

}
